---
import { getLanguageFromURL } from '../../languages';
import { SIDEBAR } from '../../config';
import logoRiots from './divRIOTS.svg?raw';
import { getCollection } from 'astro:content';

type Props = {
	currentPage: string;
};

const { currentPage } = Astro.props as Props;
const currentPageMatch = currentPage.endsWith('/')
	? currentPage.slice(1, -1)
	: currentPage.slice(1);
const langCode = getLanguageFromURL(currentPage);
const sidebar = SIDEBAR[langCode];

// Dev logs
// Get all entries from a collection. Requires the name of the collection as an argument.
const logs = (await getCollection('devlog')).sort( (a, b) => 
	b.data.date.getTime() - a.data.date.getTime()
);

const isDevLog = currentPageMatch.startsWith('devlog/');
---

<nav aria-labelledby="grid-left">
	<div class="tabs">
		<button id="button-docs" class={ !isDevLog ? 'active' : '' }>Docs</button>		
		<button id="button-devlog" class={ isDevLog ? 'active' : '' }>Devlog</button>
	</div>
	<ul id="tab-docs" class={ "nav-groups" + (!isDevLog ? ' active' : '') }>
		{Object.entries(sidebar).map(([header, children]) => (
			<li>
				<div class="nav-group">
					<h2>{header}</h2>
					<ul>
						{children.map((child) => {
							let url = child.link.startsWith('https://') ? child.link : Astro.site?.pathname + child.link;
							if (!url.endsWith('/')) url += '/';
							return (
								<li class="nav-link">
									<a href={url} aria-current={currentPageMatch === child.link ? 'page' : false}>
										{child.text}
									</a>
								</li>
							);
						})}
					</ul>
				</div>
			</li>
		))}
		<li>
			<h2>Brought to you by</h2>
			<a class="div-logo" href="https://divRIOTS.com" target="_blank" set:html={logoRiots}></a>	
		</li>
	</ul>
	<ul id="tab-devlog" class={ "nav-groups" + (isDevLog ? ' active' : '') }>
		<div class="nav-group">
			{ logs.map(log => (
				<li class="nav-link devlog-link">
					<a href={'/devlog/'+log.slug} aria-current={currentPageMatch === 'devlog/'+log.slug ? 'page' : false}>
						<time>{log.data.date.toISOString().slice(0, 10)}</time>
						{ log.data.title }
					</a>
				</li>
			)) }
		</div>
	</ul>
</nav>

<script is:inline>
	window.addEventListener('DOMContentLoaded', () => {
		var target = document.querySelector('[aria-current="page"]');
		if (target && target.offsetTop > window.innerHeight - 100) {
			document.querySelector('.nav-groups').scrollTop = target.offsetTop;
		}
	});
</script>

<script>
	// Overly complex tab management
	const buttonDocs = document.getElementById('button-docs');
	const buttonDevlog = document.getElementById('button-devlog');
	const tabDocs = document.getElementById('tab-docs');
	const tabDevlog = document.getElementById('tab-devlog');
	const onTabClink = (e) => {
		if (e.target.classList.contains('active')) return;
		buttonDocs?.classList.toggle('active');
		buttonDevlog?.classList.toggle('active');
		tabDocs?.classList.toggle('active');
		tabDevlog?.classList.toggle('active');
	}
	buttonDocs?.addEventListener('click', onTabClink);
	buttonDevlog?.addEventListener('click', onTabClink);
</script>

<style lang="scss">
	.tabs {
		display: flex;
		justify-content: space-evenly;

		& > button {
			display: block;
			width: 100%;
			text-align: center;
			margin: auto;
			padding: 6px 0;
			background-color: transparent;
			border-radius: 0;
			border-bottom: 3px solid;
			border-color: transparent;
			font-size: 1.1rem;

			&.active {
				border-color: var(--theme-accent);
			}
		}
	}

	h2 {
		font-size: 0.9rem;
		font-weight: normal;
		text-transform: uppercase;
		opacity: 0.6;
	}

	nav {
		width: 100%;
		max-height: 100vh;
		margin-left: 1rem;
		margin-right: 1rem;
		background-color: var(--theme-bg);
		overflow-y: scroll;
	}

	.nav-groups {
		display: none;
		&.active {
			display: block;
		}
	}

	.nav-groups > li + li {
		margin-top: 2rem;
	}

	.nav-groups > :first-child {
		padding-top: var(--doc-padding);
	}

	.nav-groups > :last-child {
		padding-bottom: 2rem;
		margin-bottom: var(--theme-navbar-height);
	}

	.nav-group-title {
		font-size: 1rem;
		font-weight: 700;
		padding: 0.1rem 1rem;
		text-transform: uppercase;
		margin-bottom: 0.5rem;
	}

	.nav-link a {
		font-size: 1rem;
		margin: 1px;
		padding: 0.3rem 1rem;
		font: inherit;
		color: inherit;
		text-decoration: none;
		display: block;
	}

	.nav-link a:hover,
	.nav-link a:focus {
		background-color: var(--theme-bg-hover);
	}

	.nav-link a[aria-current='page'] {
		color: var(--theme-text-accent);
		background-color: var(--theme-bg-accent);
		font-weight: 600;
	}

	.nav-link a > time {
		display: block;
		font-size: 0.8rem;
		opacity: 0.6;
	}

	@media (min-width: 50em) {
		.nav-groups {
			padding: 0;
		}
	}

	.div-logo {
		color: inherit;
		margin-left: 1rem;
	}
</style>

<style is:global>
	:root.theme-dark .nav-link a[aria-current='page'] {
		color: hsla(var(--color-base-white), 100%, 1);
	}
</style>
